package com.spdbcc;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * java8 四大函数式接口
 */
public class FunctionInterfaceTest {

    public void testPredict(){
        List<String> list = Arrays.asList("Hello", "atguigu", "Lambda", "www", "ok");
        
    }

    /**
     * 函数式接口作为参数，判断第一个参数的元素属性
     * @param list
     * @param predicate
     */
    public void userPredict(List<String> list, Predicate<String> predicate){
        List<String> strList = new ArrayList<>();
        list.forEach(e -> {
            if(predicate.test(e)){
                strList.add(e);
            }
        });

    }



    public static void main(String args[]){
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        List<Integer> squaresList = null;//getSquares(numbers);
        Stream<Integer> distinct = numbers.stream().map(i -> i * i).distinct();
        squaresList = numbers.stream().map( i ->i*i).distinct().collect(Collectors.toList());
        System.out.println("Squares List: " + squaresList);
       // System.out.println("列表: " +integers);

        List<People> peoples = Arrays.asList(
                new People("zs", 25, "cs"),
                new People("ls", 28, "bj"),
                new People("ww", 23, "nj")
        );

        Map<String, List<People>> stringListMap = peoples.stream().collect(Collectors.groupingBy(People::getName));
        System.err.println(stringListMap);
        String asss = "<div id=\"down_link\"><a href=\"down2-1905634.html\" rel=\"nofollow\" title=\"进入2.rar下载页面\" class=\"down_now\"><span>进入2.rar下载页面</span></a></div>";
        // List -> String
        List<String> names = peoples.stream().map(p -> p.getName()).collect(Collectors.toList());
        // stream流实现英文字母转大写
        List<String> upNames = names.stream().map(String::toUpperCase).collect(Collectors.toList());
        // stream流实现数字乘倍数
        List<Integer> ages = peoples.stream().map(p -> p.getAge() * 2).collect(Collectors.toList());
        // list - > new List
        List<PeoplePub> peoplePubs = peoples.stream().map(p -> {
            return new PeoplePub(p.getName(), p.getAge());
        }).collect(Collectors.toList());

        System.out.println("to print upnames List : " + upNames);
        System.out.println("to print ages List : " + ages);
        System.out.println("to print new people List" + peoplePubs.toString());


        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("map1", "aa");
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("map2", "bb");
        map2.put("map1", "cc");
        map1.putAll(map2);
        System.out.println(map1);
    }


    private static class People{
        private String name;
        private Integer age;
        private String address;

        // 只给出构造方法，忽略get/set细节
        public People(String name, Integer age, String address) {
            this.name = name;
            this.age = age;
            this.address = address;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        @Override
        public String toString() {
            return "People{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", address='" + address + '\'' +
                    '}';
        }
    }

    public static class PeoplePub{

        private String name;
        private Integer age;

        // 只给出构造方法，忽略get/set细节
        public PeoplePub(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        // 重写toString方法
        public String toString(){
            return "(" + this.name + "," + this.age + ")";

        }

    }


}
